<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="wrap">
        <button class="btn1">替换节点</button>
        <ul class="list">
            <li class="first">1</li>
            <li>2</li>
            <li class="mid">3</li>
            <li>4</li>
            <li>5</li>
            <li class="last">6</li>
        </ul>
    </div>
</body>
<script>
    // 节点
    // 文档节点  元素节点 属性节点 文本节点 注释节点

    var ul = document.getElementsByClassName("list")[0];
    var mid = document.getElementsByClassName("mid")[0];
    var btn1 = document.getElementsByClassName("btn1")[0];


    // 如何替换节点? 
    // ParentNode.replaceChild(newNode,oldNode)  在匹配的元素中,用一个节点(新的/页面中已存在的)替换另一个节点


    // 点击按钮  替换mid
    btn1.onclick = function () {
        if(mid){
            var li = document.createElement("li");

            li.innerText = "我是新增的li";
            li.style.width = Math.round(Math.random() * 100 + 50) + "px";
            li.style.height = Math.round(Math.random() * 100 + 50) + "px";
            li.style.backgroundColor = randColor();

            li.onclick = function () {
                console.log(this);
            }
            console.log(li);
            ul.replaceChild(li,mid);

            mid = null;
        }
    }




    function randColor() {
        var str = "0123456789abcdef";

        var colorStr = "#";
        for (var i = 0; i < 6; i++) {
            var index = Math.floor(Math.random() * str.length);
            var char = str.charAt(index);
            colorStr += char;
        }
        return colorStr;
    }



</script>

</html>